# 对象编程
class Student(object):
    """docstring for Student"""
    __slots__ = ('name', 'score')
    count = 0
    # 初始化方法，实例变量就直接在初始化方法中定义，类似java中的private 成员变量
    def __init__(self, name, score):
        Student.count += 1
        self.name = name
        self.score = score

    # toString方法
    def __str__(self):
        return 'Student object (name: %s, score: %s)' % (self.name, self.score)   

    __repr__ = __str__
    # 属性的getter方法
    @property
    def scorer(self):
        return self.score
    
    # 属性的setter方法
    @scorer.setter
    def scorer(self, score):
        if 0 <= score <= 100:
            self.score = score
        else:
            raise ValueError('bad score')

    def print_score(self):
        print('%s: %s' % (self.name, self.score))

def get_grade(self):
        if self.score >= 90:
            return 'A'
        elif self.score >= 60:
           return 'B' 

# bart = Student('Bart Simpson', 62)
# bart.print_score()
# print(bart.get_grade())
# print(bart.name)
# print(dir(bart))

# 测试:
if Student.count != 0:
    print('测试失败!')
else:
    bart = Student('Bart',62)
    if Student.count != 1:
        print('测试失败!')
    else:
        lisa = Student('Bart',70)
        print('开始测试getter/setter')
        print(lisa.scorer)
        lisa.scorer = 20
        print(lisa.scorer)
        print('开始测试str')
        print(lisa)
        lisa
        # 设置了slots就不可以这样用了
        # lisa.sex = 'female'
        # print(lisa.sex)
        if Student.count != 2:
            print('测试失败!')
        else:
            print('Students:', Student.count)
            print('测试通过!')